//
//  ZSPickerView.m
//  VideoExam
//
//  Created by apple on 2020/4/7.
//

#import "ZSPickerView.h"
@interface ZSPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong)UIPickerView * pickerView;
@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,strong)NSString * selectedContent;
@property(nonatomic,assign)PickerSelectType  selectType;
@property(nonatomic,strong)NSMutableArray * dataSource;
@end
@implementation ZSPickerView



- (instancetype)initWithFrame:(CGRect)frame selectType:(PickerSelectType)type dataArray:(NSMutableArray *)dataArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSource=dataArray;
        self.selectType=type;
        [self setUI];
    }
    return self;
}
- (void)setUI {
    [self initData];
    [self addTopBarView];
    [self addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(250);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
}

- (void)addTopBarView {
    UIView * topView = [[UIView alloc] initWithFrame:CGRectZero];
    topView.backgroundColor=color(@"#FAFAFA");
    topView.layer.cornerRadius = 10;
    topView.layer.qmui_maskedCorners = QMUILayerMinXMinYCorner | QMUILayerMaxXMinYCorner;
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom).offset(-250);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectZero];
    line.backgroundColor = color(@"#F0F0F0");
    [topView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(topView.mas_bottom);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(topView.mas_right);
        make.height.mas_equalTo(0.5);
    }];
    
    UIButton * btn = [UIFactory create_btnWithTitle:@"取消" imageName:nil backgroundColor:nil TypefaceColor:RGBA(0, 0, 0, 0.45) fontSize:16 target:self action:@selector(btnClcicked:)];
    [topView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(2);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    UIButton * btn2 = [UIFactory create_btnWithTitle:@"确定" imageName:nil backgroundColor:nil TypefaceColor:theme_color fontSize:16 target:self action:@selector(btnClcicked:)];
   [topView addSubview:btn2];
   [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
       make.right.mas_equalTo(topView.mas_right).offset(-15);
       make.top.mas_equalTo(2);
       make.size.mas_equalTo(CGSizeMake(40, 40));
   }];
    
}
-(void)initData {
    self.dataArray=[NSMutableArray new];
    NSArray * data;
    if (self.selectType==PickerSelect_Sex_type) {
        data = @[@"男",@"女",];
    } else if (self.selectType == PickerSelect_Data_type) {
        data=self.dataSource;
    } else if (self.selectType == PickerSelect_nationality_type) {
        //国籍
        NSString * str =@"中国,美国,德国,法国,英国,⽇本,意⼤利,加拿⼤,韩国,卢森堡,瑞⼠,挪威,爱尔兰,澳⼤利亚,荷兰,瑞典,奥地利,芬兰,⽐利时,新西兰,西班⽛,葡萄⽛,其他";
         data = [str componentsSeparatedByString:@","];
    } else if (self.selectType == PickerSelect_Nation_type) {
        NSString * str = @"汉族、蒙古族、回族、藏族、维吾尔族、苗族、彝族、壮族、布依族、朝鲜族、满族、侗族、瑶族、白族、土家族、哈尼族、哈萨克族、傣族、黎族、僳僳族、佤族、畲族、高山族、拉祜族、水族、东乡族、纳西族、景颇族、柯尔克孜族、土族、达斡尔族、仫佬族、羌族、布朗族、撒拉族、毛南族、仡佬族、锡伯族、阿昌族、普米族、塔吉克族、怒族、乌孜别克族、俄罗斯族、鄂温克族、德昂族、保安族、裕固族、京族、塔塔尔族、独龙族、鄂伦春族、赫哲族、门巴族、珞巴族、基诺族、其他";
           data = [str componentsSeparatedByString:@"、"];
    }
    [self.dataArray addObjectsFromArray:data];
    self.selectedContent=self.dataArray.firstObject;
}
- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
        _pickerView.backgroundColor=[UIColor whiteColor];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
    
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArray.count;
}
//列显示的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component {
    return self.dataArray[row];
}
#pragma mark UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.selectedContent=self.dataArray[row];
}
- (void)btnClcicked:(UIButton *)btn {
    self.backgroundColor=[UIColor clearColor];
    if ([btn.titleLabel.text isEqualToString:@"取消"]) {
       [ZSPickerView refresh_currant_tableView];
//        if (self.completeBlock) {
//            self.completeBlock(@"");
//        }
      [UIView animateWithDuration:0.5 animations:^{
                  self.top=SCREEN_HEIGHT;
            } completion:^(BOOL finished) {
                 [self removeFromSuperview];
            }];
        
    } else {
        if (self.completeBlock) {
            self.completeBlock(self.selectedContent);
        }
        [UIView animateWithDuration:0.5 animations:^{
                    self.top=SCREEN_HEIGHT;
            } completion:^(BOOL finished) {
                   [self removeFromSuperview];
            }];
    }
}
#pragma mark 刷新当前tableView
+ (void)refresh_currant_tableView {
    UIViewController *current_vc = [AppTools zs_getCurrentViewController];
    UITableView *tableView =[current_vc valueForKey:@"tableView"];
    if (tableView) {
        [tableView reloadData];
    }
}

+ (void)showSelectType:(PickerSelectType)type data:(nullable NSMutableArray *)data CompleteBlock:(void(^)(NSString *content))completeBlock {
    if (data.count==0 && type == PickerSelect_Data_type) {
        [QMUITips showInfo:@"暂无此类信息"];
        [ZSPickerView refresh_currant_tableView];
        return;
    }
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    ZSPickerView * selfView = [[ZSPickerView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) selectType:type dataArray:data];
    selfView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.4];
    [window addSubview:selfView ];
    selfView.completeBlock = completeBlock;
}

@end
